#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :Untitled-2
@说明    :
@时间    :2020/04/26 10:55:33
@作者    :王舰航
@版本    :3.0
'''

# download the mp3 files...

import requests
from bs4 import BeautifulSoup
from urllib.parse import quote
import re
# import wget
import os

url = 'http://www.listeningexpress.com/studioclassroom/ad/'
mp3_urls = []

pattern = r'sc\-ad[\s\S]{1,100}\.mp3'

home_page = requests.get( url )
page_text = home_page.text
soup = BeautifulSoup( page_text, 'html.parser' )

# print( page_text )
atags = soup.find_all( 'a' )
for atag in atags:
    if '.mp3' in atag.string:
        mp3_urls.append( url + quote( re.search( pattern, atag.get( 'href' ) ).group(0) ) )

# print( mp3_urls )
for mp3 in mp3_urls:
    # wget.download( mp3, mp3.split('/')[-1] )
    os.system( 'wget ' + mp3 + ' ' + mp3.split('/')[-1] )
